package com.lottery.im.module.result.adapter.result;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.CountDownTimer;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.ResultInfo;
import com.lottery.im.constant.Constants;
import com.lottery.im.constant.RxBusCode;
import com.lottery.im.module.result.adapter.result.item.Horizontal115Adapter;
import com.lottery.im.module.result.adapter.result.item.HorizontalFastAdapter;
import com.lottery.im.module.result.adapter.result.item.HorizontalLotteryAdapter;
import com.lottery.im.module.result.adapter.result.item.HorizontalPkAdapter;
import com.lottery.im.util.TimerUtils;
import com.lottery.im.util.rxbus.RxBus;
import java.util.List;

/**
 * Created by ztz on 2018/10/8.
 */

public class MainResultAdapter extends BaseAdapter {
  private List<ResultInfo> mData;
  public static final int COUNT_DOWN_INTERVAL = 1000;
  private Context mContext;
  private Callback mCallback;

  public interface Callback {
    void itemClick(int position);
  }

  public void onItemClick(int position) {
    mCallback.itemClick(position);
  }

  public MainResultAdapter(Context mContext, List<ResultInfo> mData, Callback callback) {
    this.mData = mData;
    this.mContext = mContext;
    mCallback = callback;
  }

  @Override
  public int getCount() {
    return mData.size();
  }

  @Override public ResultInfo getItem(int position) {
    return mData.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View v, ViewGroup parent) {
    final ViewHolder holder;
    if (v == null) {
      v = LayoutInflater.from(mContext).inflate(R.layout.item_result_list, parent, false);
      holder = new ViewHolder();
      holder.card = v.findViewById(R.id.card_view);
      holder.name = v.findViewById(R.id.tv_game_name);
      holder.sueId = v.findViewById(R.id.tv_game_sueId);
      holder.tvLh = v.findViewById(R.id.tv_game_lh);
      holder.tvHe = v.findViewById(R.id.tv_he_zhi);
      holder.tvDes = v.findViewById(R.id.tv_he_des);
      holder.mListView = v.findViewById(R.id.horizontal_list);
      holder.time = v.findViewById(R.id.tv_game_time);
      v.setTag(holder);
    } else {
      holder = (ViewHolder) v.getTag();
    }
    holder.update(position);
    return v;
  }

  class ViewHolder {
    protected CardView card;
    protected TextView name;
    protected TextView sueId;
    protected TextView tvLh;
    protected TextView tvHe;
    protected TextView tvDes;
    protected RecyclerView mListView;
    protected TextView time;
    protected MyCountDownTimer mCountDownTimer;

    @SuppressLint("ClickableViewAccessibility") private void update(int position) {
      name.setText(mData.get(position).getGameName());
      sueId.setText(mData.get(position).getIsSueId());
      String gameType = mData.get(position).getGameType();
      tvLh.setText(mData.get(position).getLongHu());
      tvHe.setText(mData.get(position).getHeZhi());
      tvDes.setText("总和:");
      LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
      linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
      mListView.setLayoutManager(linearLayoutManager);
      card.setOnClickListener(v -> onItemClick(position));
      mListView.setOnTouchListener((v, event) -> {
        if (event.getAction() == MotionEvent.ACTION_UP) {
          onItemClick(position);//模拟父类控件的点击,解决点击事件冲突
        }
        return false;
      });
      List<String> its = mData.get(position).getStringStr();
      if (Constants.lotteryArr.contains(gameType)) {
        mListView.setAdapter(new HorizontalLotteryAdapter(its));
      } else if (Constants.fastArr.contains(gameType)) {
        mListView.setAdapter(new HorizontalFastAdapter(its));
      } else if (Constants.pkArr.contains(gameType)) {
        tvDes.setText("冠亚和:");
        mListView.setAdapter(new HorizontalPkAdapter(its));
      } else if (Constants.sd115Arr.contains(gameType)) {
        mListView.setAdapter(new Horizontal115Adapter(its));
      } else if (Constants.bj28Arr.contains(gameType)) {
        mListView.setAdapter(new HorizontalLotteryAdapter(its));
      }
      long closeTime = mData.get(position).getGMTcloseTime();
      long curTime = mData.get(position).getGMTcurTime();
      String isResult = mData.get(position).getIsResult();
      if (closeTime - curTime > 0 && "1".equals(isResult)) {
        if (mCountDownTimer != null) {
          mCountDownTimer.cancel();
        }
        mCountDownTimer = new MyCountDownTimer(time, closeTime - curTime, COUNT_DOWN_INTERVAL);
        mCountDownTimer.start();
      } else {
        if (mCountDownTimer != null) {
          mCountDownTimer.cancel();
        }
        time.setText(R.string.opening);
      }
    }
  }

  private class MyCountDownTimer extends CountDownTimer {
    private TextView textView;

    MyCountDownTimer(TextView v, long millisInFuture, long countDownInterval) {
      //微调防止跳过
      super(millisInFuture + millisInFuture * 2 / countDownInterval, countDownInterval);
      textView = v;
    }

    @Override
    public void onTick(long millisUntilFinished) {
      if (textView == null) return;
      textView.setText(TimerUtils.formatDuring2(millisUntilFinished));
    }

    @Override
    public void onFinish() {
      textView.setText(R.string.opening);
      RxBus.get().send(RxBusCode.COUNT_DOWN_CODE);
    }
  }
}
